<?php

namespace App\Http\Controllers\Domain;

use App\Http\Controllers\Controller;
use App\Models\CharSpell;
use App\Models\Game\item_template;
use App\Models\Goods;
use App\Models\Player;
use Illuminate\Http\Request;
use App\Models\Server;
use App\Helper;
use App\Models\Account;
use App\Models\Character;
use App\Models\Char;
use App\Http\Requests\PlayerChangePwdRequest;
use Pay;
use App\Events\PlayerMoneyChangeEvent;


class MallSpellController extends Controller
{
    public $prefix='domain';

    public function index($domain,Request $request){
        $server=Server::where('domain',$domain)->first();
        Helper::setDataBaseConf('mysql_world',$server,'acore_world');
        $list=$server->goods;

        Helper::setDataBaseConf('mysql_char',$server,'acore_characters');
        $acc_id=json_decode( session('player'))->id;
        $characters=Character::where('account',$acc_id)->orderBy('logout_time','desc')->get();

        // 余额
//        Helper::setDataBaseConf('mysql_auth',$server,'acore_auth');
//        $money = Account::find(session('player')->id)->money;

        return view($this->prefix.'.user.mallspell',compact('server','list','characters'));
    }
    public function getData($domain){
        $server=Server::where('domain',$domain)->first();
        Helper::setDataBaseConf('mysql_world',$server,'acore_world');
        $spellMall=$server->spellMall;


        Helper::setDataBaseConf('mysql_char',$server,'acore_characters');
        $acc_id=json_decode( session('player'))->id;
        $characters=Character::where('account',$acc_id)->orderBy('logout_time','desc')->get();

        foreach ($characters as $character){
            $character->char=Char::getChar($server,$character->guid);
            $character->char->spells;
        }
        // 余额
//        Helper::setDataBaseConf('mysql_auth',$server,'acore_auth');
//        $player = Player::find(session('player')->id);

        return [
            'err'=>0,
            'data'=>compact('spellMall','characters')
        ];
    }

    public function buyPoint($domain ,Request $request){
        $value=$request->get('value');
        $guid=$request->get('guid');

        $server=Server::where('domain',$domain)->first();
        // 余额
//        Helper::setDataBaseConf('mysql_auth',$server,'acore_auth');
//        $player = Player::find(session('player')->id);
        Helper::setDataBaseConf('mysql_char',$server,'acore_characters');
        $char=Character::where('guid',$guid)->first();
        $acc=session('player');
        $acc_id=$acc->id;
        $username=$acc->username;
        if ($char && $char->account==$acc_id){
            Helper::setDataBaseConf('mysql_auth',$server,'acore_auth');
            $player = Player::getPlayer($server,$username);
            $price=$value*$server->spellMall->spellPointRatio;
            $char=Char::where('server_id',$server->id)->where('character_guid',$guid)->first();
            if ($player->money<$price){
                return [
                    'err'=>1,
                    'msg'=>'积分不足',
                    'data'=>compact('value','guid','player','char','price')
                ];
            }
            if ($char){
                $player->money -=$price;
                $player->save();
                $char->spellPoint+=$value;
                $char->save();
            }

            return [
                'err'=>0,
                'data'=>compact('value','guid','player','char','price')
            ];
        }
        return ['err'=>1,'msg'=>'错误'];
    }


    protected  $spell=null;
    function findSpell($spellTree,$spellId){
        if ($spellTree && $this->spell==null){
            foreach ($spellTree as $spell){
                if ($spell->spellId==$spellId){
                    $this->spell=$spell;
                }else{
                    $this->findSpell($spell->children,$spellId);
                }
            }
        }
    }

    public function buy($domain ,Request $request){

        $spellId=$request->get('spellId');
        $char=$request->get('char');

        if (!$char) {
            return ['err'=>-1,'msg'=>'请选择角色'];
        }

        $server=Server::where('domain',$domain)->first();

        $res='';$err='';
        $server->RunCommand("saveall",$res,$err);
        Helper::setDataBaseConf('mysql_char',$server,'acore_characters');
        $character=Character::find($char['character_guid']);
        if (!$character->online){
            return ['err'=>-1,'msg'=>'角色必须在游戏中，请登录游戏'];
        }

        $spellMall=$server->spellMall->toArray();
        $this->spell=null;
        //在公共技能中查找技能
        $this->findSpell(json_decode($spellMall['spellTree0']??'[]'),$spellId);

        if (!$this->spell){
            //在职业技能中查找技能
            $this->findSpell(json_decode($spellMall['spellTree'.$character->class]??'[]'),$spellId);
        }
        if (!$this->spell){
            return ['err'=>-1,'msg'=>'无效的技能'];
        }else{
            //检测用户是否符合条件
            if ($this->spell->reqLevel>1){
                if ($character->level<$this->spell->reqLevel){
                    return ['err'=>-1,'msg'=>'等级不够'];
                }
            }
            if ($this->spell->reqPoint>0){
                $char=Char::find($char['id']);
                if (!$char){
                    return ['err'=>-1,'msg'=>'无效的角色'];
                }
                if ($char->spellPoint<$this->spell->reqPoint){
                    return ['err'=>-1,'msg'=>'技能点不足'];
                }
            }

            $server->RunCommand("player learn $character->name $spellId",$res,$err);
            $char->spellPoint-=$this->spell->reqPoint;
            $char->save();
            $cs = new CharSpell();
            $cs->char_id=$char->id;
            $cs->spell_id=$spellId;
            $cs->price=$this->spell->reqPoint;
            $cs->save();

            return ['err'=>0,'msg'=>'','data'=>$cs];
        }


//        $price = $goods->price * $count;
//        if ($goods && $goods->server_id===$server->id){
//            $char=Character::where('guid',$guid)->first();
//            $acc=session('player');
//            $acc_id=$acc->id;
//            $username=$acc->username;
//            if ($char && $char->account==$acc_id){
//                Helper::setDataBaseConf('mysql_auth',$server,'acore_auth');
//                $player = Player::getPlayer($server,$username);
//                if ($price > $player->money){
////                    return back()->with('msg','抱歉，您的余额不足');
//                    return ['err'=>-1,'msg'=>'您的积分不足'];
//                }else{
//                    $res='';
//                    $err='';
////                    $server->SendItemToPlay($char->name,'商城购物','感谢支持!',$goods->item_id,$count,$res,$err);
//                    $char->SendItem($server,'商城购物','感谢支持!',$goods,$count,$res,$err);
//                    if ($err) {
////                        return back()->with('msg',$err);
//                        return ['err'=>-1,'msg'=>$err];
//                    }else{
//                        $player->money -= $price;
//                        $player->save();
//                        // $user = User::find($server->user_id);
//                        // $user->remain_money += $price;
//                        // $user->save();
//                        // event(new ManagerMoneyChangeEvent($server->user_id,$server->id,$acc_id,5,1,$price,0,$user->remain_money,'玩家'.$player->username.'使用'.$price.'积分购买道具'));
//                        event(new PlayerMoneyChangeEvent($server->user_id,$server->id,$acc_id,2,2,$price,0,$player->money,'使用'.$price.'积分购买道具',$goodsId));
////                        return back()->with('msg','购买成功:'.$res);
//                        return ['err'=>0,'msg'=>'购买成功：'.$res,'data'=>$player->money];
//                    }
//                }
//            }else{
////                return back()->with('msg','非法请求');
//                return ['err'=>-1,'msg'=>'非法请求'];
//            }
//        }else{
////            return back()->with('msg','没有此商品');
//            return ['err'=>-1,'msg'=>'没有此商品'];
//        }
    }

}
